package com.findmeastore.action;

import java.util.List;

import com.findmeastore.bc.IUserBc;
import com.findmeastore.bo.UserBo;
import com.findmeastore.dto.UserDto;

public class UserCrudAction {
	private Integer id;
	private UserDto userDto;
	private List<UserBo> users;
	private IUserBc userBc;
	private String action;
	
	public String execute() {
		if ("save".equals(action)) {
			return save();
		} else if ("list".equals(action)) {
			return list();
		} else if ("edit".equals(action)) {
			return edit();
		}
		return list();
	}
	
	public String list() {
		setUsers(userBc.getAllUsers());
		return "LIST";
	}

	public String edit() {
		if (id != null && id > 0) {
			UserBo userBo = userBc.getUserById(id);
			if (userBo != null) {
				userDto = new UserDto();
				userDto.setId(id);
				userDto.setUsername(userBo.getUserName());
				userDto.setPassword("*******");
				userDto.setEmail(userBo.getEmail());
				userDto.setMobile(userBo.getMobile());
			}
		}
		return "EDIT";
	}

	public String save() {
		if (userDto.getId() == null) {
			userDto.setCreatedById(1);
		}
		userDto.setModifiedById(1);
		userBc.saveUser(userDto);
		return "REDIRECT";
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<UserBo> getUsers() {
		return users;
	}

	public void setUsers(List<UserBo> users) {
		this.users = users;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public UserDto getUserDto() {
		return userDto;
	}

	public void setUserDto(UserDto userDto) {
		this.userDto = userDto;
	}

	public IUserBc getUserBc() {
		return userBc;
	}

	public void setUserBc(IUserBc userBc) {
		this.userBc = userBc;
	}
}
